package com.zhihao.takeaway.controller;

import com.zhihao.takeaway.common.utils.EasyUITable;
import com.zhihao.takeaway.common.utils.JwtUtil;
import com.zhihao.takeaway.common.utils.MD5Utils;
import com.zhihao.takeaway.common.utils.Result;
import com.zhihao.takeaway.entity.Employee;
import com.zhihao.takeaway.entity.EmployeeAndOrders;
import com.zhihao.takeaway.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/Employee")
public class EmployeeController  {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private HttpServletRequest request;


    @PostMapping("/getUserName")
    public Result getUserName(){
        Result result = new Result();
        String username = employeeService.selectUserName();
        if(username != null){
            result.success("用户已登陆");
            result.setData(username);
        }else {
            result.fail("用户未登录");
        }
        return result;
    }
    @GetMapping
    public Result getAllData(Integer currentPage){
        Result result = new Result();
        String EmployeeId = getEmployeeId();
        if(EmployeeId != null){
            EasyUITable easyUITable = employeeService.getAllData(currentPage);
            result.success("成功查询");
            result.setData(easyUITable);
        }else{
            result.fail("用户未登录");
        }
        return result;
    }
    @GetMapping("/getEmployeeBaseInfo")
    public Result getEmployeeBaseInfo(){
        Result result  = new Result();
        String EmployeeId = getEmployeeId();
        if(EmployeeId == null ||EmployeeId.equals("")){
            result.fail("亲爱的骑手，你当前处于未登录状态，请重新登录");
        }else{
            Employee employee = employeeService.getById(EmployeeId);
            result.success("查询成功");
            result.setData(employee);
        }
        return result;
    }
    @PostMapping("/updateEmployee")
    public Result updateEmployee(@RequestBody Employee employee){
        Result result  = new Result();
        String EmployeeId = getEmployeeId();
        if(EmployeeId == null||EmployeeId.equals("")){
            result.fail("亲爱的骑手，你当前处于未登录状态，请重新登录");
        }else{
            employee.setUpdateUser(EmployeeId);
            Integer num = employeeService.updateEmployee(employee);
            if(num == 1){
                result.success("更新成功");
            }else{
                result.fail("更新失败，请重试");
            }
        }

        return result;
    }
    @GetMapping("/deleteById")
    public Result deleteById(String id){
        Result result  = new Result();
        String EmployeeId = getEmployeeId();
        if(EmployeeId != null) {
            Integer num = employeeService.deleteById(id);
            result.success("删除成功");
        }else{
            result.fail("用户未登录");
        }
        return result;
    }
    @GetMapping("/getEmployeeInfo")
    public Result getEmployeeInfo(){
        Result result  = new Result();
        String EmployeeId = getEmployeeId();
        if(EmployeeId == null ||EmployeeId.equals("")){
            result.fail("亲爱的骑手，你当前处于未登录状态，请重新登录");
        }else{
            EmployeeAndOrders employeeAndOrders = employeeService.getEmployeeInfo(EmployeeId);
            result.success("个人信息查询成功");
            result.setData(employeeAndOrders);
        }
        return result;
    }

    /**
     * 这个方法是chat中使用的，一般axios没有用到过，不要修改
     * @param employeeId
     * @param session
     * @return
     */
    @PostMapping("/getEmployeeInfoById")
    public Result getEmployeeInfoById(String employeeId, HttpSession session){
        Result result  = new Result();
        String UserId = getUserId();
        if(UserId == null || UserId.equals(""))
        {
            result.fail("当前用户未登录，请及时登录");
        }else
        {
            session.setAttribute("myId",UserId);
            Employee employee = employeeService.getById(employeeId);
            result.setData(employee);
            result.success("成功获取");
        }
        return result;
    }

    ////////////////////////////  聊天系统  查询   特殊处理////////////////////////////////////////
    @GetMapping("/getEmployeeSimpleInfo")
    public Result getEmployeeSimpleInfo(String employeeId){
        Result result  = new Result();
        Employee employee = employeeService.getById(employeeId);
        result.setData(employee);
        result.success("成功获取");
        return result;
    }

    public String getEmployeeId(){
        String token = request.getHeader("token");
        String EmployeeId = JwtUtil.validateToken(token);
        return EmployeeId;
    }
    public String getUserId(){
        String token = request.getHeader("token");
        String UserId = JwtUtil.validateToken(token);
        return UserId;
    }
}
